package com.zlh123.controller;

import com.zlh123.commom.util.PageResult;
import com.zlh123.commom.util.R;
import com.zlh123.entity.Book;
import com.zlh123.service.IBookService;
import com.zlh123.vo.BookVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zlh
 * @since 2025-01-13
 */
@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    private IBookService bookService;
    //book列表
    @RequestMapping("/list")
    public R getBRList() {
        return R.ok().data("list", bookService.list());
    }
    //分页
    @GetMapping("/findBooksByPage/{page}/{pageSize}")
    public R findByPage(@PathVariable("page") int page,
                        @PathVariable("pageSize") int pageSize) {
        PageResult<Book> student=bookService.findBooksByPage(page,pageSize);
        return R.ok().data("list",student);
    }
    //条件查询并分页
    @PostMapping("/searchBook/{page}/{pageSize}")
    public R searchBook(@PathVariable("page") int page,
                    @PathVariable("pageSize") int pageSize,
                    @RequestBody(required = false) BookVo bookVo) {
        PageResult<Book> data=bookService.searchBook(page,pageSize,bookVo);
        return R.ok().data("list",data);
    }
    // 添加书籍
    @PostMapping("/add")
    public R addBook(@RequestBody Book book) {
        boolean flag = bookService.addBook(book);
        return flag ? R.ok().message("添加书籍信息成功!") : R.error().message("添加书籍信息失败!");
    }

    //修改学生
    @PutMapping("/update")
    public R update(@RequestBody Book book){
        boolean flag = bookService.updateById(book);
        return flag ? R.ok().message("修改成功!") : R.error().message("修改失败!");
    }
    // 删除学生
    @DeleteMapping("/delete/{ids}")
    public R delete(@PathVariable("ids") String ids){  // 1,2,3
        String[] split = ids.split(",");
        List<String> sids = Arrays.asList(split);
        return bookService.removeByIds(sids) ? R.ok().message("删除书籍信息成功!") : R.error().message("删除失败!");
    }
}
